package com.zz.examination.controller;

import com.zz.examination.core.Result;
import com.zz.examination.core.ResultGenerator;
import com.zz.examination.entity.User;
import com.zz.examination.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserRepository userRepository;

    @PostMapping("")
    public Result login(String userName) {
        if (StringUtils.isBlank(userName)) {
            return ResultGenerator.genFailResult("用户名不能为空");
        }
        User user = userRepository.getOrSave(userName);
        return ResultGenerator.genSuccessResult(user);
    }
}
